package com.atguigu.juc.cas;

import java.util.concurrent.atomic.AtomicReference;

/**
 * @author yanglin
 * @create 2024-01-24 16:20 星期三
 * description:原子引用
 */
public class AtomicReferenceDemo {

    public static void main(String[] args) {
        AtomicReference<User> userAtomicReference = new AtomicReference<>();
        User zhangsan = new User("张三", 18);
        User lisi = new User("李四", 28);
        userAtomicReference.set(zhangsan);     //封装一个原子引用类

        boolean b = userAtomicReference.compareAndSet(zhangsan, lisi);
        System.out.println(b);
        User user = userAtomicReference.get();
        System.out.println(user);
    }
    
    
}


class User {
    String userName;
    int age;

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public User() {
    }

    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }

    @Override
    public String toString() {
        return "User{" +
                "userName='" + userName + '\'' +
                ", age=" + age +
                '}';
    }
}
